/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.InventoryControl;

import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import merlion.mrp.pmm.entity.FinishedGood;
import merlion.scm.icm.entity.ProductInventoryRecord;
import merlion.scm.icm.session.InventoryControlSessionBeanLocal;
import org.primefaces.context.RequestContext;

@ManagedBean(name = "piManagerBean")
@ViewScoped
public class ProductInventoryManagerBean {

    @EJB
    private InventoryControlSessionBeanLocal icSessionBean;
    private ProductInventoryRecord pir;
    private FinishedGood fg;
    private String statusMsg;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------    
    public ProductInventoryManagerBean() {
        pir = new ProductInventoryRecord();
        fg = null;
    }

    @PostConstruct
    public void init() {
    }

    //--------------------------------------------------------------------------
    //                              Manager Bean Methods
    //--------------------------------------------------------------------------
    public Collection<ProductInventoryRecord> getAllProductInventoryRecord() {
        return icSessionBean.getAllProductInventoryRecord();
    }
    
    public void editOptimalLevel(ActionEvent event){
        pir = (ProductInventoryRecord) event.getComponent().getAttributes().get("selected");
        fg = null;
    }

    public void viewProductInventoryRecord(ActionEvent event) {
        pir = (ProductInventoryRecord) event.getComponent().getAttributes().get("selected");
        fg = null;
    }
    
    public void viewBatchLocation(ActionEvent event) {
        fg = (FinishedGood) event.getComponent().getAttributes().get("selected");
    }

    public String saveProductInventoryGroup() {

        RequestContext context = RequestContext.getCurrentInstance();

        //Validate
        boolean valid = true;
        if (pir.getOptimalStock() < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Optimal Stock cannot be negative", ""));
            valid = false;
        }
        context.addCallbackParam("valid", valid);

        if (valid) {
            pir = icSessionBean.saveProductInventoryRecord(pir);

            if (pir != null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Product Inventory " + pir.getProduct().getProductType() + " updated successfully.", ""));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Product Inventory update has failed. Please try again.", ""));
            }
        }
        
        return null;
    }

    //--------------------------------------------------------------------------
    //                              Getter and Setter
    //--------------------------------------------------------------------------
    public ProductInventoryRecord getPir() {
        return pir;
    }

    public void setPir(ProductInventoryRecord pir) {
        this.pir = pir;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public FinishedGood getFg() {
        return fg;
    }

    public void setFg(FinishedGood fg) {
        this.fg = fg;
    }
}
